package com.example.service.impl.redis;

import com.example.pojo.entity.redis.Test;
import com.example.service.redis.TestService;
import com.example.util.common.UuidGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author LiYuhang
 * @version 0.1
 * @application
 * @Date 2020/9/23 11:19
 */
@Service
@Slf4j
public class TestServiceImpl implements TestService {

    @Override
    @Caching(
            /**
             * Cacheable[] cacheable() default {};
             * 	CachePut[] put() default {};
             * 	CacheEvict[] evict() default {};
             * 	存放 test::#{test.id}
             * 	删除 test::#{test.code}
             *
             */
            put = {
                    @CachePut(value = "test", key = "#test.id"),
            },
            evict={
                    @CacheEvict(value = "test", key = "#test.code"),
            }
    )
    public void save(Test test) {
        log.info("save Test:{}",test.toString());
    }
    @Caching(
            /**
             * 	 存放 test::#{test.id}
             *   删除 test::#{test.code}
            */
            put = {
                    @CachePut(value = "test", key = "#test.id"),
            },
            evict={
                    @CacheEvict(value = "test", key = "#test.code"),
            }
    )
    @Override
    public void update(Test test) {
        log.info("update Test:{}",test.toString());
    }

    @Override
    public List<String> getDataList() {
        int pageSize=20;
        List<String> list = new ArrayList<>(pageSize);

        for (int i = 0; i < pageSize; i++) {
            list.add(UuidGenerator.generatorUuid());
        }
        return list;
    }

    @Override
    public boolean writToDb() {
        return true;
    }
}
